package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.Service.OrderDetailService;
import com.ithiema.reggie.Service.OrderService;
import com.ithiema.reggie.Service.ShoppingCartService;
import com.ithiema.reggie.common.BaseContext;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.dto.OrdersDto;
import com.ithiema.reggie.entity.OrderDetail;
import com.ithiema.reggie.entity.Orders;
import com.ithiema.reggie.entity.ShoppingCart;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    private R<String> submit (@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }


    //订单明细(打开出页面)
    @GetMapping("/page")
    private R<Page> page (int page, int pageSize,Integer number,String beginTime,String endTime){
        //分页条件构建器
        Page pageinfo = new Page(page,pageSize);
        LambdaQueryWrapper<Orders> lwp = new LambdaQueryWrapper<>();
        //gt.lt在这个时间之内
        lwp.like(number!=null,Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);

        lwp.orderByDesc(Orders::getCheckoutTime);

        //切记操作完了后要.{上传}
        orderService.page(pageinfo,lwp);

        return R.success(pageinfo);
    }


    //手机端查看订单
    @GetMapping("/userPage")
    private R<Page> upage (int page,int pageSize){
        Page pageinfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> lwp1 = new LambdaQueryWrapper<>();
        //还要比较当前id
        lwp1.eq(Orders::getUserId, BaseContext.getCurrentId());
        lwp1.orderByDesc(Orders::getOrderTime);
        orderService.page(pageinfo,lwp1);

        Page<OrdersDto> ordersDtoPage = new Page();
        BeanUtils.copyProperties(pageinfo,ordersDtoPage,"records");


        //从数据库中查询到的集合，不包含分类名称
        List<Orders> records = pageinfo.getRecords();
        //创建需要返回的所有订单数据集合
        List<OrdersDto> list = new ArrayList<>();

        records.stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            //拷贝
            BeanUtils.copyProperties(item, ordersDto);
            //根据订单号查询订单详情表数据
            LambdaQueryWrapper<OrderDetail> lwp2 = new LambdaQueryWrapper<>();
            lwp2.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> detailList = orderDetailService.list(lwp2);
            //设置订单对应的订单详情数据
            ordersDto.setOrderDetails(detailList);
            //添加进返回的订单数据集合
            list.add(ordersDto);
            return item;
        }).collect(Collectors.toList());


        //把需要返回的所有订单数据集合，设置进返回对象中
        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }


    //订单明细里的修改派送
    @PutMapping
    public R<String> ss(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> lwp = new LambdaUpdateWrapper<>();
        lwp.eq(orders.getId()!=null,Orders::getId,orders.getId());
        lwp.set(Orders::getStatus,orders.getStatus());
        orderService.update(lwp);
        return R.success("修改成功");
    }

//    //手机端在来一单   方法一:
//    @PostMapping("/again")
//    private R<String> zai(@RequestBody Map<String,Long> map){
//        log.info("接收到前端传递的id{}",map.get("id"));
//        //获取订单明细表中的id
//
//        LambdaQueryWrapper<OrderDetail> lwp1 = new LambdaQueryWrapper<>();
//        lwp1.eq(OrderDetail::getOrderId,map.get("id"));
//
//        List<OrderDetail> orderDetailList = orderDetailService.list(lwp1);
//        List<ShoppingCart> list= orderDetailList.stream().map(item ->{
//            //购物车
//            ShoppingCart shoppingCart = new ShoppingCart();
//            //获取购物车表的全部信息
//            shoppingCart.setName(item.getName());
//            shoppingCart.setImage(item.getImage());
//            //获取当前di
//            shoppingCart.setUserId(BaseContext.getCurrentId());
//            shoppingCart.setDishId(item.getDishId());
//            shoppingCart.setSetmealId(item.getSetmealId());
//            shoppingCart.setDishFlavor(item.getDishFlavor());
//            shoppingCart.setNumber(item.getNumber());
//            shoppingCart.setAmount(item.getAmount());
//            //获取当前时间
//            shoppingCart.setCreateTime(LocalDateTime.now());
//
//            return shoppingCart;
//
//        }).collect(Collectors.toList());
//
//
//        shoppingCartService.saveBatch(list);
//        return R.success("成功下单");
//    }


    //再来一单 {适合我的方法}
    @PostMapping("/again")
    private R<String> dan(@RequestBody Orders orders){

        LambdaQueryWrapper<OrderDetail> lwp = new LambdaQueryWrapper<>();
        lwp.eq(OrderDetail::getOrderId,orders.getId());

        List<OrderDetail> list = orderDetailService.list(lwp);
        //这里用购物车  因为要把东西存到购物车
        List<ShoppingCart> list1 = list.stream().map(it ->{
            ShoppingCart shoppingCart = new ShoppingCart();

            //获取购物车表的全部信息
            shoppingCart.setName(it.getName());
            shoppingCart.setImage(it.getImage());
            //获取当前di
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setDishId(it.getDishId());
            shoppingCart.setSetmealId(it.getSetmealId());
            shoppingCart.setDishFlavor(it.getDishFlavor());
            shoppingCart.setNumber(it.getNumber());
            shoppingCart.setAmount(it.getAmount());
            //获取当前时间
            shoppingCart.setCreateTime(LocalDateTime.now());

            return shoppingCart;
        }).collect(Collectors.toList());

        shoppingCartService.saveBatch(list1);
        return R.success("再来一单成功");
    }


}